﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb
{
    public partial class AdminMaster : System.Web.UI.MasterPage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Lang"] == null)
            {
                Session["Lang"] = Global.LangDefault;
            }
            if (Page.User.Identity.Name == null || Page.User.Identity.Name == "")
            {
                GlobalClass.ErrorMessage("authentication");
            }
            else
            {
                if (Session["UserName"] == null || Session["UserName"].ToString() == "")
                {
                    List<User> list = new List<User>();
                    list = UserService.User_GetByUsername(Page.User.Identity.Name);
                    if (list.Count > 0)
                    {
                        Session["UserId"] = list[0].Id.Trim();
                        Session["FullName"] = list[0].Name.Trim();
                        Session["UserName"] = list[0].Username.Trim();
                        Session["IsAdmin"] = list[0].Admin;
                    }
                }
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string permission = Business.ModuleService.Module_GetPermissionForUser(Request.Url.PathAndQuery.Replace(GlobalClass.GetUrlAdmin(),string.Empty), Session["UserId"].ToString(), Session["IsAdmin"].ToString());
            string currentPage = Request.Url.PathAndQuery.ToLower();
            if (!currentPage.Contains("changepass.aspx"))
            {
                ControlClass.SetPermistionControlForUser(MainContent, permission);
            }
            if (permission.Equals("0") && (!currentPage.Contains("error.aspx") && !currentPage.Contains("default.aspx") && !currentPage.Contains("changepass.aspx")))
            {
                HttpContext.Current.Response.Redirect(GlobalClass.GetUrlAdmin() + "Error.aspx");
            }
        }
    }
}